package com.k2091.washcar.controller;


import com.k2091.washcar.entity.*;
import com.k2091.washcar.service.IAdminService;
import com.k2091.washcar.service.ITokenService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private ITokenService tokenService;

    /**
     * 管理员添加用户
     * @param users
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public int addUser(@RequestBody Users users) {
        int r = adminService.insertUser(users);
        return r;
    }

    //管理员修改用户信息
    @RequestMapping("/updateUser")
    public int update(@RequestBody Users users){
        int r = adminService.updateUser(users);
        return r;
    }

    //管理员添加洗车员
    @RequestMapping("/addCleaner")
    @ResponseBody
    public int addCleaner(@RequestBody Cleaner cleaner){
        int r = adminService.insertCleaner(cleaner);
        return r;
    }

    //管理员修改洗车员信息
    @RequestMapping("/updateCleaner")
    @ResponseBody
    public int updateCleaner(@RequestBody Cleaner cleaner){
        int r = adminService.updateCleaner(cleaner);
        return r;
    }

    //冻结用户
    @RequestMapping("/freezeUser")
    @ResponseBody
    public int fUser(@RequestBody Users user){
        int r = adminService.freezeUser(user.getId());
        return r;
    }

    //冻结洗车员
    @RequestMapping("/freezeCleaner")
    @ResponseBody
    public int fCleaner(@RequestBody Cleaner cleaner){
        int r = adminService.freezeCleaner(cleaner.getId());
        return 0;
    }

    @RequestMapping("/login")
    @ResponseBody
    public JSONObject adminLogin(RuleForm ruleForm){
        System.out.println(ruleForm);
        String token = ruleForm.getName()+ruleForm.getPassword();
        HashMap<String,Object> hashMap = new HashMap();
        if (adminService.selectByNameAndPassword(ruleForm.getName(),ruleForm.getPassword())!=null){
            Token tokenEntity = new Token();
            tokenEntity.setToken(token);
            tokenService.addToken(tokenEntity);
            hashMap.put("token",token);
            hashMap.put("success",true);
            hashMap.put("msg","登录成功");
        }else {
            hashMap.put("success",false);
            hashMap.put("msg","登录失败");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulateAll(hashMap);
        System.out.println(jsonObject);
        return jsonObject;
    }

    @RequestMapping("/getAdminList")
    public List<Admin> getAdminList(FormInline formInline){
        System.out.println(formInline);
        return adminService.selectAdmin(formInline);
    }

    @RequestMapping("/deleteAdmin")
    public void deleteUser(Long id,String token){
        adminService.deleteAdmin(id);
    }

    @RequestMapping("/addAdmin")
    public void addUser(Admin admin){
        adminService.insertAdmin(admin);
        Long id = admin.getId();
        if (adminService.selectAdminById(id)==null){
            adminService.insertAdmin(admin);
        }else {
            adminService.upAdmin(admin);
        }
    }

    @RequestMapping("/upStatus")
    public void upStatus(Long id,int status,String token){
        adminService.upStatus(id,status);
    }
}
